#include <sys/types.h>
#include <assert.h>
#include <stdlib.h>
#include "converters.h"

struct mediafs_decoder_ctx *
mediafs_decoder_open(struct mediafs_decoder *decoder, char const *stream_contents, size_t stream_size)
{
	struct mediafs_decoder_ctx *result;

	assert(decoder != NULL);
	assert(stream_contents != NULL);
	assert(stream_size > 0);

	result = decoder->ctx_open_func(stream_contents, stream_size);
	result->decoder = decoder;

	return (result);
}

void
mediafs_decoder_close(struct mediafs_decoder_ctx *ctx)
{
	assert(ctx != NULL);

	ctx->decoder->ctx_close_func(ctx);
}

int
mediafs_decoder_get_stat(struct mediafs_decoder_ctx *ctx, struct mediafs_decoder_stat *stat)
{
	assert(ctx != NULL);
	assert(stat != NULL);

	return (ctx->decoder->ctx_get_stat_func(ctx, stat));
}

int
mediafs_decoder_decode(struct mediafs_decoder_ctx *ctx, uint64_t samples_from, uint64_t samples_duration, char *buffer)
{
	assert(ctx != NULL);
	assert(buffer != NULL);

	return (ctx->decoder->ctx_decode_func(ctx, samples_from, samples_duration, buffer));
}

struct mediafs_encoder_ctx *
mediafs_encoder_open(struct mediafs_encoder *encoder, struct mediafs_encoder_params *params)
{
	struct mediafs_encoder_ctx *ctx;

	assert(encoder != NULL);
	assert(params != NULL);

	ctx = encoder->ctx_open_func(params);
	ctx->encoder = encoder;
	return ctx;
}

void
mediafs_encoder_close(struct mediafs_encoder_ctx *ctx)
{
	assert(ctx != NULL);
	assert(ctx->encoder != NULL);

	ctx->encoder->ctx_close_func(ctx);
}

int
mediafs_encoder_get_stat(struct mediafs_encoder_ctx *ctx, struct mediafs_encoder_stat *stat)
{
	assert(ctx != NULL);
	assert(ctx->encoder != NULL);
	assert(stat != NULL);

	return ctx->encoder->ctx_get_stat_func(ctx, stat);
}

int
mediafs_encoder_write_header(struct mediafs_encoder_ctx *ctx, char *buffer, size_t *buffer_size)
{
	assert(ctx != NULL);
	assert(ctx->encoder != NULL);
	assert(buffer_size != NULL);

	return ctx->encoder->ctx_write_header_func(ctx, buffer, buffer_size);
}

int
mediafs_encoder_encode(struct mediafs_encoder_ctx *ctx, uint64_t frames_offset, uint64_t frames_duration,
	char *src_buffer, char *dest_buffer, size_t *dest_buffer_size)
{
	assert(ctx != NULL);
	assert(ctx->encoder != NULL);
	assert(src_buffer != NULL);
	assert(dest_buffer != NULL);
	assert(dest_buffer_size != NULL);

	return ctx->encoder->ctx_encode_func(ctx, frames_offset, frames_duration, src_buffer, dest_buffer, dest_buffer_size);
}

